/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Java_Nio_Charset.java]
 * Creation Date        [06-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_api_testing;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Map;

/**
 * java.nio.Charset.Charset
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_Nio_Charset {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		// 获得当前系统可用的字符集
		Map<String, Charset> availableCharsets = Charset.availableCharsets();
		for (Map.Entry<String, Charset> entry : availableCharsets.entrySet()) {
			System.out.println(entry.getKey());
		}

		System.out.println();

		// 获取指定名称的字符集
		Charset charset = Charset.forName("UTF-8");
		System.out.println(charset.aliases());

		// 编码Unicode字符串
		String helloWorld = "Hello World";
		ByteBuffer buffer = charset.encode(helloWorld);
		byte[] bytes = buffer.array();
		System.out.println(Arrays.toString(bytes));

		// 解码Unicode字符串
		buffer = ByteBuffer.wrap(bytes, 0, bytes.length - 1);
		CharBuffer cbuffer = charset.decode(buffer);
		System.out.println(cbuffer.toString());
	}

}
